﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMKCBBanDau : DBInteractionBase
    {public string MaKCBBanDau { get; set; }
        public string TenKCBBanDau { get; set; }
        public string DiaChi { get; set; }
        public bool IsActive { get; set; }

        public bool Insert(DMKCBBanDau obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKCBBanDau_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKCBBanDau", ConvertNvarcharToVarchar(obj.MaKCBBanDau)));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKCBBanDau", obj.TenKCBBanDau));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMKCBBanDau obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKCBBanDau_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKCBBanDau", obj.MaKCBBanDau));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKCBBanDau", obj.TenKCBBanDau));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKCBBanDau_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKCBBanDau", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKCBBanDau_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKCBBanDau", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMKCBBanDau_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMKCBBanDau obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMKCBBanDau_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKCBBanDau", string.IsNullOrEmpty(obj.MaKCBBanDau) ? "" : obj.MaKCBBanDau));
                    dbcmd.Parameters.Add(new SqlParameter("@TenKCBBanDau", string.IsNullOrEmpty(obj.TenKCBBanDau) ? "" : obj.TenKCBBanDau));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMKCBBanDau_SelectAll: " + ex.Message);
            }
        }
    }
}
